<?php
/* @var $eblast Eblast */
/* @var $model Technical */
?>
<div class="left">
    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id' => 'ad-form',
	'enableAjaxValidation' => false,
	'enableClientValidation' => true,
	'clientOptions' => array(
	    'validateOnSubmit' => true
	),
	'htmlOptions' => array(
	    'enctype' => 'multipart/form-data'
	)
    ));
    ?>

    <div class="title">Post Technical Service</div>

    <?php echo $form->errorSummary($model) ?>
    <?php echo $form->errorSummary($eblast) ?>
    <?php if (Yii::app()->user->hasFlash('featureOverLimit')): ?>        
	<?php echo Yii::app()->user->getFlash('featureOverLimit'); ?>        
    <?php endif; ?>

    <div class="sign-1 in-div">
	<div>
	    <h4>Your ad category</h4>
	    <div class="clear"></div>
	    <?php echo $form->dropDownList($model, 'main_category', CHtml::listData(Category::getList(), 'category_id', 'name')); ?>

	    <?php $this->renderPartial("//ad/register/_duration"); ?>
	    <p class="show-price"><label>Price: $<span id="subPrice"><?php ?></span></label></p>
	</div>
	<div class="otherCat">
	    <h4>E-blast this campaign to another category</h4>
	    <div class="clear"></div>

	    <?php foreach (Category::getList() as $cat) : ?>
    	    <label class="checkbox">
    		<input type="checkbox" value="<?php echo $cat->category_id ?>" class="price" name="Technical[otherCategoryIds][]" <?php echo in_array($cat->category_id, $model->otherCategoryIds) ? 'checked' : '' ?> /><?php echo h($cat->name) ?>, Price: $<?php echo number_format($cat->price_addition) ?>
    	    </label>
	    <?php endforeach; ?>
	    <div>Price: <span id="total-price-cat"></span></div>
	</div>
	<div class="clear"></div>
	<div class="total-price">Total Price: $<span id="totalPrice"></span></div>

	<div class="clearfix"></div>
	<div>
	    <label>Upload your company logo</label>
	    <?php echo $form->fileField($model, 'image') ?>
	</div>
    </div>

    <div class="sign-1 in-div">
	<div>	    
	    <label class="checkbox">
		<input id="enable-eblast" type="checkbox" name="eblast" <?php if (isset($_POST['eblast'])) echo 'checked' ?> /><strong>E-blast your company advertising campaign</strong>
	    </label>
	</div>
	<div>
	    <label><strong>Upload e-blast advertisement</strong></label>
	    <?php echo $form->fileField($eblast, 'advertisement') ?>
	</div>


	<div style="margin-top:10px;">
	    <h4>E-blasting Category</h4>
	    <div class="clear"></div>
	    <?php echo $form->dropDownList($eblast, 'main_category', CHtml::listData(Category::getList(), 'category_id', 'name')); ?>

	    <br>
	    <h4>E-blast My Campaign Date</h4>
	    <?php
	    echo $form->datepickerRow($eblast, 'start_date', array(
		'prepend' => '<i class="icon-calendar"></i>'
	    ))
	    ?>
	    <br>
	    <?php
//	    echo $form->datepickerRow($eblast, 'end_date', array(
//		'prepend' => '<i class="icon-calendar"></i>'
//	    ))
	    ?>
	    <div>Price: <span class="price-e-blast"></span></div>
	</div>
	<div style="margin-top:10px;">
	    <h4>List this Ad in another category. </h4>
	    <div class="clear"></div>
	    <?php foreach (Category::getList() as $cat) : ?>
    	    <label class="checkbox">
    		<input type="checkbox" value="<?php echo $cat->category_id ?>" class="more-eblast-cat" name="Eblast[otherCategoryIds][]" <?php echo in_array($cat->category_id, $model->otherCategoryIds) ? 'checked' : '' ?> /><?php echo h($cat->name) ?>, Price: $<?php echo number_format($cat->price_addition) ?>
    	    </label>
	    <?php endforeach; ?>
	    <div>Price: <span class="price-e-blast-more"></span></div>
	</div>

	<div class="clear"></div>

	<div class="total-price" style="margin-bottom:10px;">Total Price: $<span class="total-price-e-blast">0</span></div>



    </div>

    <div class="sign-1 in-div">

	<div style="width:600px; float:none;">
	    <strong>Featured Advertiser</strong><br>	    
	    <i>
		Become one of our Monthly featured Advertiser. Spots are limited; we only accommodate 6 advertisers per month. Book your spot now and let the printing industry know about your offerings!
	    </i>
	    <br>
	    <label class="radio inline" style="padding-left:0px;">
		<strong>Time: </strong>
	    </label>

	    <label class="radio inline">
		<?php echo $form->checkBox($model, 'featured') ?> 1 Month
	    </label>

	    <label class="radio inline">
		<strong>Price</strong>: $<span id="total-price-featured"><?php echo settings()->get('post_price', 'featured'); ?></span>
	    </label>
	</div>


	<div class="line-ke"></div>
	<div>
	    <label>UpLoad Advertisement </label>
	    <?php echo $form->fileField($model, 'advertisement') ?>
	</div>

    </div>

    <div class="sign-1 in-div">
	<h4>Contact Informations</h4>
	<p><em>All contact information will not be posted and is used for communication between you and buyer.</em></p>
	<div>
	    <label>Contact Name *</label>
	    <?php echo $form->textField($model, 'contact_name', array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>Email *</label>
	    <?php echo $form->textField($model, 'contact_email', array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>Country</label>
	    <?php echo $form->dropDownList($model, 'contact_country', CHtml::listData(Country::model()->findAll(array('order' => 'country_name')), 'country_iso', 'country_name'), array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>State</label>
	    <?php echo $form->textField($model, 'contact_state', array('maxlength' => 60)); ?>
	</div>

	<div>
	    <label>City</label>
	    <?php echo $form->textField($model, 'contact_city', array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>Phone number</label>
	    <?php echo $form->textField($model, 'contact_phone', array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>Cell number</label>
	    <?php echo $form->textField($model, 'contact_cellphone', array('maxlength' => 60)); ?>
	</div>
	<div>
	    <label>Website</label>
	    <?php echo $form->textField($model, 'website', array('maxlength' => 255)); ?>
	</div>
    </div><!--in-div-->

    <div class="sign-1 in-div">
	<h4>Description about Company / Services</h4>	
	<i>This information along with your company logo will be posted in our resource page</i>
	<div style="width:500px;">
	    <label>Description<span class="star">*</span></label>
	    <?php echo $form->textArea($model, 'content', array('rows' => 10, 'cols' => 50, 'class' => 'span6')); ?>
	</div>
    </div>

    <div class="sign-1 in-div">        
	<div>
	    <h4>Type of Business</h4>
	    <?php echo $form->dropDownList($model, 'businessType', Technical::getBusinessTypes()) ?>
	</div>

	<div style="width:347px;">
	    <h4>Type of Product / Services</h4>
	    <?php
	    echo $form->dropDownList($model, 'typeOfService', Technical::getServiceTypes(), array(
		'multiple' => 'multiple'
	    ))
	    ?>
	    <br>
	    <em style="font-size:12px;">(click Ctrl + items to select mulitple products/services)</em>
	</div>


    </div>
    <div class="total-price" style="margin-bottom:10px;">Total Price: <span id="total-price-company-source"></span></div>
    <div class="sign-1 in-div">
	<button type="submit" class="btn btn-warning">Post Ads</button>
    </div>
    <?php $this->endWidget(); ?>
</div>

<?php $this->renderPartial('//ad/register/_right', array("type" => "technical")) ?>

<script>
    $(document).ready(function() {
	var cat = [];
<?php foreach (Category::getList() as $cat) : ?>
    <?php /*
      cat.push({
      two_week: <?php echo $cat->price_two_week ?>,
      one_month: <?php echo $cat->price_one_month ?>,
      addition: <?php echo $cat->price_addition ?>
      });
     * 
     */ ?>
    	cat.push({
    	    price_two_week: <?php echo $cat->price_two_week ?>,
    	    price_one_month: <?php echo $cat->price_one_month ?>,
    	    price_e_blast: <?php echo $cat->price_e_blast; ?>,
    	    price_3_month: <?php echo $cat->price_3_month ?>,
    	    price_6_month: <?php echo $cat->price_6_month ?>,
    	    price_one_year: <?php echo $cat->price_one_year ?>,
    	    price_addition: <?php echo $cat->price_addition ?>
    	});
<?php endforeach; ?>

<?php
/*
 * for Price on top
 */
?>
	$('#Technical_main_category').change(function() {
	    var index = $(this).prop("selectedIndex");
	    //disable checkbox
	    $('.otherCat input[type=checkbox]').each(function(i) {
		if (i == index) {
		    $(this).attr('disabled', 'disabled');
		    $(this).attr('checked', false);
		} else {
		    $(this).removeAttr('disabled');
		}
	    }).promise().done(function() {
		calTotal();
	    });
	});
	$('input[name=duration]').change(function() {
	    calTotal();
	});
	$('input[type=checkbox].price').change(function() {
	    calTotal();
	});

	function calTotal() {
	    var type = $('input[name=duration]:checked').val();
	    //main cat
	    var index = $('#Technical_main_category').prop("selectedIndex");
	    var subPrice = cat[index][type];

	    //get checkbox
	    var otherCat = 0;
	    var numCatChose = 0;
	    $('input[type=checkbox].price:checked').each(function() {
		//get index
		var index = $('.otherCat input[type=checkbox]').index($(this));
		otherCat += cat[index].price_addition;
		numCatChose += 1;
	    }).promise().done(function() {
		$('#subPrice').html(subPrice.toFixed(2));

		if (numCatChose > 1) {
		    var discount = <?php echo settings()->get('discount', 'discount_category') ?>;
		    var finalPrice = otherCat - otherCat * (parseFloat(discount) / 100);
		    $('#total-price-cat').html("$" + finalPrice + '(discounted ' + discount + '%)');
		} else {
		    $('#total-price-cat').html("$" + otherCat);
		    finalPrice = otherCat;
		}

		$('#totalPrice').html((finalPrice + subPrice).toFixed(2));
	    });
	    callTotalAll();
	}

	//trigger
	$('#Technical_main_category').trigger('change');
<?php
/*
 * For price on E-blast
 */
?>
<?php //event click on checkbox eblast                                                                  ?>
	$('#enable-eblast').click(function() {
	    if ($(this).attr('checked')) {
		$('#Eblast_main_category').trigger("change");
	    } else {
		$('.total-price-e-blast').html('0');
	    }
	});

<?php //select main category                                                                 ?>
	$('#Eblast_main_category').change(function() {

	    var index = $(this).prop("selectedIndex");
	    //disable checkbox
	    $('.more-eblast-cat').each(function(i) {
		if (i == index) {
		    $(this).attr('disabled', 'disabled');
		    $(this).attr('checked', false);
		} else {
		    $(this).removeAttr('disabled');
		}
	    }).promise().done(function() {
		if ($('#enable-eblast').attr('checked')) {
		    calTotalEblast();
		}
	    });
	});
	$('#Eblast_main_category').trigger("change");
<?php //select main category                                                                 ?>
	$('.more-eblast-cat').change(function() {
	    if ($('#enable-eblast').attr('checked')) {
		calTotalEblast();
	    }
	});

	function calTotalEblast() {
	    //main cat
	    var index = $('#Eblast_main_category').prop("selectedIndex");
	    var priceEblast = cat[index].price_e_blast;
	    //get checkbox
	    var otherCat = 0;
	    var numCatChose = 0;
	    $('.more-eblast-cat:checked').each(function() {
		//get index
		var index = $('.more-eblast-cat').index($(this));
		console.log(index);
		otherCat += cat[index].price_addition;
		numCatChose += 1;
	    }).promise().done(function() {
		$('.price-e-blast').html(priceEblast.toFixed(2));


		if (numCatChose > 1) {
		    var discount = <?php echo settings()->get('discount', 'discount_category') ?>;
		    var finalPrice = otherCat - otherCat * (parseFloat(discount) / 100);
		    $('.price-e-blast-more').html("$" + finalPrice.toFixed(2) + '(discounted ' + discount + '%)');
		} else {
		    $('.price-e-blast-more').html("$" + otherCat.toFixed(2));
		    finalPrice = otherCat;
		}
		$('.total-price-e-blast').html((finalPrice + priceEblast).toFixed(2));

	    });

	    callTotalAll();
	}
<?php
/*
 * featured
 */
?>
	$('#Technical_featured').click(function() {
	    callTotalAll();
	});
<?php
/*
 * total of all page 
 */
?>
	function callTotalAll() {
	    var totalTop = $('#totalPrice').html();
	    var totalEblast = $('.total-price-e-blast').html();
	    var totalFeatured = 0;
	    if ($('#Technical_featured').attr('checked')) {
		var totalFeatured = $('#total-price-featured').html();
	    } else {
		var totalFeatured = 0;
	    }
	    $('#total-price-company-source').html("$" + (parseFloat(totalTop) + parseFloat(totalEblast) + parseFloat(totalFeatured)).toFixed(2));
	}
    });
</script>